var table = null;
var pid, tid;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'form', 'util'], function () {
        var form = layui.form;
        $ = layui.$;
        table = layui.table;
        let util = layui.util;
        let fillingModel;


        pid = $("#projectList").val();

        openTaskData(table, pid);

        form.render();
        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTaskData(table, pid);
        });

        form.on('submit(searchData)', function (data) {
            openArchivesRetrospect(data.field);
            return false;
        });


        //监听工具条
        table.on("row(taskTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            tid = data.id;
            fillingModel = data.fillingModel;
            openArchivesRetrospect();
        });

        function openArchivesRetrospect(data) {
            let where = {pid: pid, tid: tid};
            if (data !== undefined) {
                where = Object.assign({}, where, data);
            }
            table = layui.table;
            table.render({
                elem: '#dataTable'
                , url: '/archives/getArchivesRetrospectData'
                , contentType: "application/json"
                , method: 'post'
                , cellMinWidth: 50
                , page: true
                , limit: 50
                , height: 'full-70'
                , loading: true
                , where: where
                , size: 'sm'
                , cols: [
                    [ //标题栏
                        {type: 'checkbox', fixed: 'left'},
                        {fixed: 'left', field: 'entitystatus', title: '状态', width: 60, templet: '#entityStatusTpl'},
                        {fixed: 'left', field: 'status', title: '领卷', width: 50, templet: '#statusTpl'},
                        {field: 'batchcode', title: '批次号', fixed: 'left', width: 100},
                        {field: 'archivalcode', title: '档号', fixed: 'left', width: 180},
                        {field: 'w2', title: '整理分件', width: 70, templet: "#w2Tpl"},
                        {field: 'w18', title: '打码', width: 70, templet: "#w18Tpl"},
                        {field: 'w18', title: '盖档号章', width: 70, templet: "#w19Tpl"},
                        {field: 'w3', title: '原件录入', width: 70, templet: "#w3Tpl"},
                        {field: 'w4', title: '原件二录', width: 70, templet: "#w4Tpl"},
                        {field: 'w5', title: '图像扫描', width: 70, templet: "#w5Tpl"},
                        {field: 'w6', title: '补扫重扫', width: 70, templet: "#w6Tpl"},
                        {field: 'w7', title: '图像优化', width: 70, templet: "#w7Tpl"},
                        {field: 'w8', title: '看图录入', width: 70, templet: "#w8Tpl"},
                        {field: 'w9', title: '看图二录', width: 70, templet: "#w9Tpl"},
                        {field: 'w10', title: '双录校对', width: 70, templet: "#w10Tpl"},
                        {field: 'w11', title: '图像质检', width: 70, templet: "#w11Tpl"},
                        {field: 'w12', title: '质量检查', width: 70, templet: "#w12Tpl"},
                        {field: 'w13', title: '质量总检', width: 70, templet: "#w13Tpl"},
                        {field: 'w14', title: '质量抽检', width: 70, templet: "#w14Tpl"},
                        {field: 'w15', title: '打印封装', width: 70, templet: "#w15Tpl"},
                        {field: 'w16', title: '档案上架', width: 70, templet: "#w16Tpl"},
                        {field: 'arrangeinfo', title: '整理信息', width: 200},
                        {field: 'isback', title: '退返', width: 60, templet: "#isbackTpl"},
                        {field: 'backinfo', title: '退返原因', width: 200},
                        {field: 'backverify', title: '退返审核', width: 80, templet: "#backverifyTpl"},
                        {field: 'backverifyremark', title: '退返审核备注', width: 200},
                        {fixed: 'right', title: '操作', width: 60, toolbar: '#operationBar'}
                    ]
                ],
                done: function (res, curr, count) {
                    //表格加载完成后，默认选中第一行
                    $('.layui-table-view[lay-table-id="dataTable"]').children('.layui-table-box').children('.layui-table-body').find('table tbody tr[data-index="0"]').click();
                }
            });
        }

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "receive":
                    top.layer.open({
                        type: 2,
                        title: "领卷追溯",
                        content: '../archives/receiveLog?archivesId=' + data.archivesid,
                        area: ['600px', $(parent.window).height() + 'px'],
                        shadeClose: false,
                        resize: false,
                        offset: 'r',
                    });
                    return false;
                case  'changelog':
                    top.layer.open({
                        type: 2,
                        title: "档案追溯日志",
                        content: '../archives/changelog?archivesId=' + data.archivesid,
                        area: ['500px', $(parent.window).height() + 'px'],
                        shadeClose: false,
                        resize: false,
                        offset: 'r',
                    });
                    break;
            }
        });

        util.on("lay-action", {
            assigned: function () {
                var data = table.checkStatus('dataTable').data;
                if (data.length < 1) {
                    top.parent.layer.alert("没有选择需要分配的档案", {icon: 0});
                    return false;
                }
                var ids = [];
                for (let i = 0; i < data.length; i++) {
                    ids.push(data[i].archivesid);
                }
                top.layer.open({
                    type: 2,
                    title: '分配任务',
                    content: '../collection/setTaskAssigned?archivesModel=' + fillingModel + '&pid=' + pid + '&tid=' + tid + '&ids=' + ids.toString(),
                    area: ['628px', '480px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        var iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable');
                            layer.close(index);
                        }
                        return false;
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
            },
            return: function () {
                var data = table.checkStatus('dataTable').data;
                if (data.length < 1) {
                    top.parent.layer.alert("没有选择需要回收的档案", {icon: 0});
                    return false;
                }
                var ids = [];
                for (let i = 0; i < data.length; i++) {
                    if (data[i].status == '1') {
                        ids.push(data[i].archivesid);
                    }
                }
                //归还
                parent.layer.confirm("确定要回收所选择档案吗？", function (index) {
                    $.ajax({
                        url: "./returnWorkReceives", //url
                        contentType: "application/json",
                        dataType: "json", //数据格式
                        type: "POST", //请求方式
                        async: false, //是否异步请求
                        data: JSON.stringify({ids: ids.toString(), pid: pid, tid: tid}),
                        success: function (result) {
                            if (result.code == 0) {
                                table.reload("dataTable");
                                parent.layer.msg(result.message, {icon: 1});
                            } else {
                                parent.layer.alert(result.message, {icon: 2});
                            }
                        },
                    });
                    return false;
                });
            }

        });

    });